//
//  XLUsersReusableView.m
//  XiangLove
//
//  Created by feng song on 2018/1/25.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLUsersReusableView.h"
#import <UIImageView+YYWebImage.h>

@implementation XLUsersReusableView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        [self interView];
    }
    return self;
}
-(void)setOnlinePeople:(NSString *)onlinePeople
{
    _onlinePeople = onlinePeople;
    [self interView];
}

-(void)setUsersPicurl:(NSString *)usersPicurl
{
    _usersPicurl = usersPicurl;
    [self interView];
}

-(NSMutableArray *)onlineAry
{
    if (_onlineAry == nil) {
        _onlineAry = [[NSMutableArray alloc] init];
    }
    return _onlineAry;
}

- (void)interView
{
    [self.onlineAry removeAllObjects];
    UIImageView * headImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, XQScreenW, 200)];
    headImg.clipsToBounds = YES;
    headImg.contentMode = UIViewContentModeScaleAspectFill;
    headImg.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    [headImg setContentScaleFactor:[[UIScreen mainScreen] scale]];
    [headImg setImageWithURL:[NSURL URLWithString:self.usersPicurl] placeholder:[UIImage imageNamed:@"UserBanner"]];
    [self addSubview:headImg];
    NSLog(@"在线人数==%@",self.onlinePeople);
    if (self.onlinePeople.length >0) {
        for (int i = 0; i < self.onlinePeople.length; i++) {
            [self.onlineAry addObject:[NSString stringWithFormat:@"%c",[self.onlinePeople characterAtIndex:i]]];
        }
    }
    NSInteger numBgW = 18*self.onlineAry.count+2*(self.onlineAry.count+1);
    UIView * numBgView = [[UIView alloc] init];
    numBgView.backgroundColor = kClearColor;
    [headImg addSubview:numBgView];
    [numBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headImg.mas_top).offset(25);
        make.right.equalTo(headImg.mas_right).offset(-7);
        make.size.mas_equalTo(CGSizeMake(numBgW,18));
    }];
    for (int i = 0; i<self.onlineAry.count; i++) {
        UILabel * numTitle = [[UILabel alloc] initWithFrame:CGRectMake(2+i%self.onlineAry.count*(2+18),0,18,18)];
        numTitle.backgroundColor = RGBA_HEX(0x000000, 0.3);
        numTitle.text = self.onlineAry[i];
        numTitle.textColor = kWhiteColor;
        numTitle.textAlignment = NSTextAlignmentCenter;
        [numBgView addSubview:numTitle];
    }
    UILabel * onlineTitle = [[UILabel alloc] init];
    onlineTitle.text = @"在线人数";
    onlineTitle.textColor = kWhiteColor;
    onlineTitle.font = kSetFont(12.0);
    [headImg addSubview:onlineTitle];
    [onlineTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(numBgView);
        make.right.equalTo(numBgView.mas_left).offset(-2);
    }];
}
@end
